import Vue from 'vue'
import VueRouter from 'vue-router'
import index from "../views/Index"
// import Home from '../views/Index'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect:"index"
  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  },
  {
    path: "/index",
    component:index
  },
  {
    path: "/course",
    component:()=>import("../views/CourseDetail")
  },
  {
    path: "/mine",
    component: () => import("../views/Mine"),
    meta:{needLogin:true}
  },
]
import {getUserMsg} from "../api/api"
const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to, from, next) => {
  console.log(to)
  console.log(from)
  console.log(next)
  //哪些页面需要判断登录
  if(to.meta.needLogin){
  //  检测有没有登录
  getUserMsg().then(res=>{
      if(res.code == 0){
        next()
      }else {
        next("/index")
      }
    })

  }else {next()}


  // if(to.path == "/mine"){
  //   next("/course")
  // }else{
  //   next()
  // }

})
export default router
